{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !wget https://f000.backblazeb2.com/file/malay-dataset/parsing/indonesia-conll/Indonesian/id-common_crawl-000.conllu.jsonl\n",
    "# !wget https://f000.backblazeb2.com/file/malay-dataset/parsing/indonesia-conll/Indonesian/id-wikipedia-000.conllu.jsonl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from unidecode import unidecode\n",
    "from tqdm import tqdm\n",
    "from glob import glob\n",
    "import json\n",
    "\n",
    "def get_train(group):\n",
    "    texts, tags = [], []\n",
    "    for g in group:\n",
    "        splitted = g.split('\\t')\n",
    "        texts.append(unidecode(splitted[1]))\n",
    "        tags.append(splitted[3])\n",
    "        \n",
    "    return texts, tags"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['id-wikipedia-000.conllu.jsonl']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "files = sorted(glob('*.conllu.jsonl'))\n",
    "files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "1451051it [00:18, 80205.14it/s] \n"
     ]
    }
   ],
   "source": [
    "X, Y = [], []\n",
    "for f in ['id-wikipedia-000.conllu.jsonl']:\n",
    "    with open(f) as fopen:\n",
    "        for l in tqdm(fopen):\n",
    "            data = json.loads(l)\n",
    "            try:\n",
    "                data = [d for d in data if d[0] != '#']\n",
    "                if len(data) > 200:\n",
    "                    # print(data)\n",
    "                    continue\n",
    "                x, y = get_train(data)\n",
    "                if len(x) != len(y):\n",
    "                    continue\n",
    "                \n",
    "                X.append(x)\n",
    "                Y.append(y)\n",
    "                \n",
    "            except:\n",
    "                pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(len(X)):\n",
    "    if len(X[i]) != len(Y[i]):\n",
    "        print(X[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "train_X, test_X, train_Y, test_Y = train_test_split(X, Y, test_size = 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1160739, 290185)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(train_X), len(test_X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Pulau', 'Samalona']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_X[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['NOUN', 'PROPN']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_Y[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('prepared.json', 'w') as fopen:\n",
    "    json.dump({\n",
    "        'train_X': train_X,\n",
    "        'train_Y': train_Y,\n",
    "        'test_X': test_X,\n",
    "        'test_Y': test_Y\n",
    "    }, fopen)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
